package test2;

import java.util.Arrays;

public class MyStack {
    public int[] elem;
    public int usedSize;
    public  MyStack(){
        this.elem=new int[5];
    }

    public void push(int val){
        if(isFull()){
            //扩容
            this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[usedSize]=val;
        usedSize++;
    }
    public boolean isFull(){
        return this.elem.length==this.usedSize;
    }

    public int pop(){
        if(isEmpty()){
           throw new RuntimeException("栈为空");
        }
        int oldVal=this.elem[usedSize-1];
        usedSize--;
        return oldVal;
    }
    public boolean isEmpty(){
        return this.usedSize==0;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("栈为空");
        }
        return this.elem[usedSize-1];
    }
}
